package com.isoft.uaaservice.controller;



import com.isoft.uaaservice.bean.ResponseData;
import com.isoft.uaaservice.bean.SysUser;
import com.isoft.uaaservice.service.ISysLoginService;
import com.isoft.uaaservice.util.FileUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.security.PublicKey;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends BaseController{
    @Autowired
    ISysLoginService sysLoginService;


    /*登录*/
    @RequestMapping("/login")
    public ResponseData login(SysUser sysUser){
        if (null != sysUser){
            log.info("controller:"+sysUser);
        }
        return sysLoginService.login(sysUser);
    }

    /*退出*/
    @RequestMapping("/logout")
    public ResponseData logout(){
        return sysLoginService.logout();
    }

    /*用户注册（限定外部用户）*/
    @PostMapping("/register")
    public ResponseData<Boolean> register(SysUser sysUser){
        ResponseData<Boolean>  booleanResponseData=returnBoolean(sysLoginService.register(sysUser));
        return booleanResponseData;
    }
    /*远程访问提供注册接口（合同工普通员工）*/
    @PostMapping("/registerresttemplate")
    public Boolean registerRestTemplate(@RequestBody SysUser sysUser){
        return sysLoginService.registerRestTemplate(sysUser);
    }
    /*忘记密码*/
    //1.获取指定用户信息
    @GetMapping("/getnewone")
    public ResponseData<Object> getNewOne(SysUser sysUser){
        return returnObject(sysLoginService.getNewOne(sysUser));
    }
    //2.修改密码
    @PutMapping("/changepassword")
    public  ResponseData<Boolean> changePassword(@RequestBody SysUser sysUser){
        return returnBoolean(sysLoginService.changePassword(sysUser));
    }


    /*修改用户密码*/
    @PutMapping("/updatepassword")
    public  ResponseData<Boolean> updatePasswordById(@RequestBody SysUser sysUser){
        return returnBoolean(sysLoginService.updatePasswordById(sysUser));
    }
    /*账户冻结与启用*/
    @PostMapping("/freezeenable")
    public Boolean freezeAndEnable(@RequestBody SysUser sysUser){
        //System.out.println(sysUser);
        return sysLoginService.freezeAndEnable(sysUser);
    }

    /*修改用户头像*/
    //文件上传—将客户端文件读到项目设置路径中
    @Value("${myfile.user.photopath}")
    private String savePath;
    @PostMapping("/rephoto/{id}")
    public ResponseData<String> rePhoto(@RequestParam("userphoto") MultipartFile file , @PathVariable("id")  Integer id , HttpServletRequest request){
        boolean result = false;
        String fileUrl = null;
        String saveFileUrl = null;
        //测试
        System.out.println("前端获取id:"+id);
        System.out.println("前端获取file:"+file);
        System.out.println("前端获取request:"+request);
        if (null != file){
            //重新构造上传文件名： id_日期.扩展名
            String newName =id + "_"+
                    new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) +
                    FileUtil.getExtName(file.getOriginalFilename());
            //保存文件，得到文件保存后的url地址
            fileUrl = FileUtil.uploadFile(file,newName,savePath,request);
            System.out.println("文件路径:"+fileUrl);
            if (null != fileUrl){
                saveFileUrl=savePath + File.separator + newName;
                System.out.println("存入数据库路径:" + saveFileUrl);
                sysLoginService.changeUserPhoto(id , fileUrl);
                result=true;
            }
        }
        ResponseData<String>responseData =new ResponseData<>();
        responseData.setData(fileUrl);
        if(result) {
            responseData.setErrorCode(200);
            responseData.setMsg("成功");
        } else {
            responseData.setErrorCode(605);
            responseData.setMsg("失败");
        }
        return responseData ;
    }




}
